
from pyparsing import Keyword, Optional
from base import Base
import helper as hp

class PolicyHeader(Base):
    def __init__(self):
        super(PolicyHeader, self).__init__()
        self._header = {}

    @hp.addlinenumber
    def _addHeader(self, string, location, tokens):
        for key, value in tokens:
            if key == "logical-system":
                self._header["lsys"] = value
            else:
                self._header[key] = value

    def _util(self, string, location, tokens):
        retval = []
        if tokens[0] == "interface-specific":
            retval = ["intsp", True]

        return [retval]

    @property
    def grammar(self):
        target = hp.addStmt("target", hp.target)
        name = hp.addStmt("filter", hp.identifier)
        lsys = hp.addStmt("logical-system", hp.identifier)
        intsp = Keyword("interface-specific").setParseAction(self._util)

        hdropts = (
                hp.comment &
                target &
                name &
                Optional(lsys) &
                Optional(intsp)
                ).setParseAction(self._addHeader)

        return hp.kword("header") + hp.lb + hdropts + hp.rb

    @property
    def value(self):
        return self._header

